package com.example.a10928.weifriend.ui.personal_center

import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.util.Log
import com.example.a10928.weifriend.App

import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_bind_bank.*
import kotlinx.android.synthetic.main.activity_with_draw_activity.*

class BindBankActivity : BaseActivity() {


    var type:String=""
    var data:String=""
    override fun initData() {



    }

    override fun initView() {

        type= intent.getStringExtra(VariableName.TYPE)
        data=intent.getStringExtra(VariableName.DATA)

        if(type.equals("1")){
            title_layout.setTitle("修改银行卡")
            btn_bind_bank.text="确认修改"

        }

        supportActionBar?.hide()
        btn_bind_bank.setOnClickListener {

            if(TextUtils.isEmpty(et_name.text)){
                L.t("请填写名字")
                return@setOnClickListener
            }
            if(TextUtils.isEmpty(bankId)){
                L.t("请选择银行")
                return@setOnClickListener
            }
            if(TextUtils.isEmpty(et_bank_num.text)){
                L.t("请填写卡号")
                return@setOnClickListener
            }
            if(TextUtils.isEmpty(et_idcard.text)){
                L.t("请填写身份证号")
                return@setOnClickListener
            }
            if(TextUtils.isEmpty(et_phone.text)){
                L.t("请填写手机号码")
                return@setOnClickListener
            }


            if(type.equals("1")){
                App.app().bpService.updateBankCard(App.app().user.userId,
                        et_name.text.toString(),et_bank_num.text.toString(),bankId,
                        et_idcard.text.toString(),et_phone.text.toString(),tv_bank_name.text.toString(),
                        data)
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {

                                    L.t("修改成功")
                                    Handler().postDelayed(Runnable {
                                        finish()
                                    },500)

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }else{
                App.app().bpService.createBankCard(App.app().user.userId,
                        et_name.text.toString(),et_bank_num.text.toString(),bankId,
                        et_idcard.text.toString(),et_phone.text.toString(),tv_bank_name.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {

                                    L.t("绑定成功")
                                    Handler().postDelayed(Runnable {
                                        finish()
                                    },500)

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )
            }



        }

        rl_bank.setOnClickListener {
            var intent= Intent(this,SelectActivity::class.java)
            intent.putExtra(VariableName.TYPE, VariableName.BANK)
            startActivityForResult(intent, VariableName.REQUEST_BANK)
        }

    }

    override fun getContentView(): Int {
        return R.layout.activity_bind_bank
    }

    var bankId:String?=""

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode!= Activity.RESULT_OK){
            return
        }
        when(requestCode){
            VariableName.REQUEST_BANK-> {
                bankId= data?.getStringExtra(VariableName.ID)
                tv_bank_name.text= data?.getStringExtra(VariableName.NAME)

                Log.v("zzw","BankID"+bankId);
            }

        }



    }

}
